<?php
	defined('_JEXEC') or die('Restricted access');
	$model      = $this->data['model'];
	$motor      = $this->data['motor'];
	$user       = $this->data['user'];
	$motor_user = $this->data['motor_user'];

	$motor_keys = array(
		'kv'          => 'Частота вращения (KV)',
		'weight'      => 'Вес мотора (г)',
		'max_current' => 'Номинальный ток (А)',
		'max_voltage' => 'Максимальное напряжение (В)',
	);

	$motor_test = $model->getMotorTest($motor['id']);
	$tester     = $model->getMotorTester($motor['id']);
	$motor_set  = $model->getMotorSet($motor['id']);
	$motor_rate = $model->getMotorRate($motor['id']);
?>

<h1>База данных по моторам</h1>
<h2>Мотор "<?= $motor['title'] ?>"</h2>
<div class="rsbase_content">
	<div style="text-align:right;">
		<?php if ($user->id == $motor['user_id']): ?>
			<a href="/?option=com_rcbase&view=motor&task=editmotor&id=<?= $motor['id'] ?>">Редактировать</a>
			| <a href="/?option=com_rcbase&view=motor&task=delmotor&id=<?= $motor['id'] ?>">Удалить</a>
		<?php endif; ?>
	</div>
	<table>
		<tr>
			<td valign="top" width="510"><img src="<?= $motor['img_src'] ?>" alt="<?= $motor['title'] ?>"></td>
			<td valign="top">
				<table class="table">
					<tr>
						<th>Параметр</th>
						<th>Значение</th>
					</tr>
					<?php foreach ($motor_keys as $key => $value): ?>
						<tr>
							<td><?= $value ?></td>
							<td align="right"><?= $motor[$key] ? $motor[$key] : '&nbsp;' ?></td>
						</tr>
					<?php endforeach; ?>
					<tr>
						<td>Рейтинг мотора</td>
						<td align="right">
							<a href="/?option=com_rcbase&view=motor&task=rateup&id=<?= $motor['id'] ?>">&uArr;</a>
							<span style="color:green;" title="<?= $motor['rate_up'] ? 'Мотор понравился: '.$motor_rate['rate_up_users'] : '' ?>">
								<?= $motor['rate_up'] ? $motor_rate['rate_up'] : '0' ?>
							</span> /
							<a href="/?option=com_rcbase&view=motor&task=ratedown&id=<?= $motor['id'] ?>">&dArr;</a>
							<span style="color:red;" title="<?= $motor['rate_down'] ? 'Мотор не понравился: '.$motor_rate['rate_down_users'] : '' ?>">
								<?= $motor['rate_down'] ? $motor_rate['rate_down'] : '0' ?>
							</span>
						</td>
					</tr>
				</table>
				<?php if ($motor['product_id']): ?>
					<p>
						<table class="table">
							<tr>
								<th>Цена</th>
								<th>Наличие</th>
								<th>Продавец</th>
							</tr>
							<tr>
								<td id="rcbase_motorprice"><?= $motor['price'] ?> USD</td>
								<td id="rcbase_motorstate" align="center"><?= $motor['state'] > 10 ? '<span style="color:green;">10+</span>' : ($motor['state'] <= 0 ? '<span style="color:red;">'.$motor['state'].'</span>' : '<span style="color:orange;">'.$motor['state'].'</span>')  ?></td>
								<td>
									<a href="http://www.hobbyking.com/hobbyking/store/uh_viewItem.asp?idProduct=<?= $motor['product_id'] ?>" target="_blank">HobbyKing</a>,
									<a href="http://www.parkflyer.ru/product/<?= $motor['product_id'] ?>/" target="_blank">ParkFlyer</a>
								</td>
							</tr>
						</table>
						<script language="JavaScript" type="text/javascript">
			 				//jQuery('#rcbase_motorprice').load('http://www.hobbyking.com/hobbyking_api.asp?id=<?= $motor['product_id'] ?>&switch=3');
			 				//jQuery('#rcbase_motorstate').load('http://www.hobbyking.com/hobbyking_api.asp?id=<?= $motor['product_id'] ?>&switch=1');
			 			</script>
					</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>
	<?php if ($motor['memo']): ?>
		<p>
			<?= $motor['memo'] ?>
		</p>
	<?php endif; ?>
	<?php if ($motor['page_id']): ?>
		<p>
			К данному мотору есть обзорная статья, <a href="/?option=com_content&view=article&id=<?= $motor['page_id'] ?>">посмотреть обзор...</a>
		</p>
	<?php endif; ?>
	<p><i>Мотор добавил пользователь: <?= $motor_user->name; ?></i></p>
	<h2>Результаты тестов</h2>
	<div style="text-align:right;">
		<?php if (!$user->guest): ?>
			<a href="/?option=com_rcbase&view=motortest&task=addtest&id=<?= $motor['id'] ?>">Добавить тест</a>
		<?php endif; ?>
	</div>
	<table class="table">
		<tr>
			<th>Автор</th>
			<th>Винт</th>
			<th>АКБ</th>
			<th>Ток (А)</th>
			<th>Напр (В)</th>
			<th>Мощ. (Вт)</th>
			<th>Тяга (г)</th>
			<th>Рейтинг</th>
		</tr>
		<?php foreach ($motor_test as $test): ?>
			<tr>
				<td><?= $tester[$test['user_id']]['username'] ?></td>
				<td><?= $test['prop_type'] ?> <?= $test['prop_d'] ?>X<?= $test['prop_h'] ?></td>
				<td align="right"><?= $test['cells'] ?>S</td>
				<td align="right"><?= $test['current'] ?></td>
				<td align="right"><?= ($test['voltage'] * 1) > 0 ? $test['voltage'] : '~'.(3.7 * $test['cells']) ?></td>
				<td align="right"><?= ($test['voltage'] * 1) > 0 ? $test['voltage'] * $test['current'] : '~'.(3.7 * $test['cells'] * $test['current']) ?></td>
				<td align="right"><?= $test['power'] ?></td>
				<td align="right">
					<span style="color:green;">
						<a href="/?option=com_rcbase&view=motortest&task=rateup&mid=<?= $motor['id'] ?>&id=<?= $test['id'] ?>">&uArr;</a>
						<?= $test['rate_up'] ? count(explode('|', $test['rate_up'])) : '0' ?>
					</span>
					/
					<span style="color:red;">
						<a href="/?option=com_rcbase&view=motortest&task=ratedown&mid=<?= $motor['id'] ?>&id=<?= $test['id'] ?>">&dArr;</a>
						<?= $test['rate_down'] ? count(explode('|', $test['rate_down'])) : '0' ?>
					</span>
				</td>
			</tr>
		<?php endforeach; ?>
	</table>
	<h2>Примеры установок</h2>
	<div style="text-align:right;">
		<?php if (!$user->guest): ?>
			<a href="/?option=com_rcbase&view=motorset&task=addset&id=<?= $motor['id'] ?>">Добавить пример установки</a>
		<?php endif; ?>
	</div>
	<table class="table">
		<tr>
			<th>Автор</th>
			<th>Модель</th>
			<th>Класс</th>
			<th>Вес (г)</th>
			<th>Винт</th>
			<th>АКБ</th>
			<th>ESC (А)</th>
			<th>Рейтинг</th>
		</tr>
		<?php foreach ($motor_set as $set): ?>
			<tr>
				<td><?= $tester[$set['user_id']]['username'] ?></td>
				<td><?= $set['model_name'] ?></td>
				<td><?= $set['model_type_title'] ?></td>
				<td><?= $set['model_weight'] ?></td>
				<td><?= $set['prop_type'] ?> <?= $set['prop_d'] ?>X<?= $set['prop_h'] ?></td>
				<td align="right"><?= $set['cells'] ?>S</td>
				<td align="right"><?= $set['esc_current'] ?></td>
				<td align="right">
					<span style="color:green;">
						<a href="/?option=com_rcbase&view=motorset&task=rateup&mid=<?= $motor['id'] ?>&id=<?= $set['id'] ?>">&uArr;</a>
						<?= $set['rate_up'] ? count(explode('|', $set['rate_up'])) : '0' ?>
					</span>
					/
					<span style="color:red;">
						<a href="/?option=com_rcbase&view=motorset&task=ratedown&mid=<?= $motor['id'] ?>&id=<?= $set['id'] ?>">&dArr;</a>
						<?= $set['rate_down'] ? count(explode('|', $set['rate_down'])) : '0' ?>
					</span>
				</td>
			</tr>
		<?php endforeach; ?>
	</table>
</div>